
let formatData = formatDataPlay(dataInit);
function Box(elem, options, data) {
    // autoPlay 是否开启自动轮播
    // interval 自动轮播间隔时间
    this.defaults = {
        autoPlay: true,
        interval: 1, // s
    }; // 默认配置

    this.opts = $.extend({}, this.defaults, options); // 在jQuery命名空间上增加两个函数
    this.interval = this.opts.interval * 1000;
    this.autoPlay = this.opts.autoPlay;
    this.curent = 0;
    this.$container = elem.find('.percent_Box');
    this.$count = this.$container.find('.count');
    this.$curent = this.$container.find('.curent');
    this.$active = this.$container.find('.active');
    this.$percent = this.$container.find('.percent');
    this.data = data;
    this.carouselList = [];
    this.$textBox = this.$container.find('.innerBox');
    this.inital();

    // console.log(this.$count, this.$curent, this.$active, this.$percent);
}

Box.prototype = {
    play: function () {
        if (this.autoPlay) {
            this.initalCarousel();
        }
    },

    // 执行渲染
    initalCarousel: function () {

        let date = new Date();
        // 筛选当天数据
        let curMinutes = formatDateToMin([date.getHours(),date.getMinutes()].join(':'))
        let day = date.getDay() ===0 ? 7 : date.getDay() ;
        let curData = this.data[day-1];
        // 获取当前时间节点的数据
        let list = _.takeRight(curData.data.filter(item => item.startTime<=curMinutes), 1);
        if(list.length>0 &&list[0].curPlay !== this.curent){
            this.$count.html(curData.count);
            this.curent = list[0].curPlay;
            this.$curent.html(list[0].curPlay);
            this.$active.css({
                width: list[0].curPlay/curData.count*100 + '%'
            });
            this.$percent.html((list[0].curPlay/curData.count*100).toFixed(1));
            carousel(this.$textBox, list[0].list)
        }
        let carouselList = list[0].list.filter(item => item.endTime > curMinutes);
        if(list[0].curPlay !== this.curent){
            carousel(this.$textBox, carouselList);
        }else if(list[0].curPlay === this.curent && this.carouselList.length !== carouselList.length){
            carousel(this.$textBox, carouselList);
        }
        this.carouselList = carouselList;
        this.curent = list[0].curPlay;

    },

    inital: function () {
        let self = this;

        this.initalCarousel();
        clearInterval(this.timer);
        this.timer = setInterval(function () {
            self.play();
        }, this.interval);
    },

    constructor: Box
};
this.Box = new Box(this.getContainer(), {
    autoPlay: true,
    interval: 2,  // 秒单位
}, formatData);
function carousel(elem, data) {
    // 追加操作，无缝轮播
    data = data.concat(data[0]).map(item => {
        return {
            name: item['节目名称'],
            subTitle: item['播出频道1'],
        }
    });
    let $textBox = elem;
    $textBox.html('');
    data.forEach(function (item) {
        $textBox.append('<div class="fontShadow item"><div class="textSub">'+ item.subTitle +'</div><div class="textMain">' + item.name + '</div></div>');
    });

    let items = $textBox.find('.item'); // 轮播循环体
    let height = 120;
    let itemsLen = data.length;

    let interval = 4000; // interval 自动轮播间隔时间
    let autoPlay = true; // autoPlay 是否开启自动轮播
    let run = null;
    let i = 0;

    autoPlay && auto();
    function auto() {
        clearInterval(run);
        run = setInterval(function () {
            i++;
            if (i === itemsLen) {
                $textBox.css({ marginTop: 0 });
                i = 1
            }
            move();
        }, interval);
    }
    function move() {
        $textBox.stop().animate({
            marginTop: -height * i
        }, 500, 'swing');
    }
    $textBox.hover(function () {
        autoPlay && clearInterval(run);
    }, function () {
        autoPlay && auto();
    });
}

function formatDateToMin (time) {
    if(_.isEmpty(time)){
        return 0;
    }
    let arr = time.split(':');
    if(arr.length < 2){
        return 0;
    }
    return Number(arr[0]) * 60 + Number(arr[1]);
}
function formatDataPlay(data){
    let weekData = Array.from(new Array(7)).map((item, index) => {
        return {
            day: index + 1,
            data: []
        };
    });

    // 按天分组， 并将时间转化成分钟
    data.forEach(item => {
        let dayArr = item['播出日期'].split('、').map(k => Number(k));
        dayArr.forEach((v, i) => {
            weekData[v - 1].data.push({
                ...item,
                startTime: formatDateToMin(item['播出(首播)时间']),
                endTime: formatDateToMin(item['播出结束时间']),
            });
        });
    });
    // 按时间分组
    let finialData = [];
    weekData.forEach(item => {
        let count = item.data.length;
        // 将每天的数据按 -录制时间- 排序
        let itemData = _.sortBy(item.data, [function (o) { return o.startTime }]);
        let time = [];
        let timeData = [];
        itemData.forEach((i, k) => {
            let order = time.indexOf(i.startTime);
            if(order === -1){
                time.push(i.startTime);
                timeData.push({
                    startTime: i.startTime,
                    curPlay: k + 1,
                    list: [i]
                });
            } else {
                timeData[order].list.push(i);
            }
        });
        finialData.push({
            day: item.day,
            count: count,
            data: timeData
        });
    });
    return finialData;
}


